package com.victor.controller.web;

import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.victor.entity.Bookcase;
import com.victor.entity.Comments;
import com.victor.service.BookcaseService;
import com.victor.service.CommentsService;
import com.victor.service.YhUsersService;
import com.victor.utils.Msg;

@RestController
@RequestMapping("/comments")
public class CommentsController {

	@Autowired
	private CommentsService commentService;

	@Autowired
	private BookcaseService bookService;

	@Autowired
	private YhUsersService yhService;

	@Autowired
	private PlatformTransactionManager txManager;

	/**
	 * 获取绘本评论
	 * 
	 * @param id
	 * @return
	 */
	@GetMapping("/getComments/{pid}")
	public Msg getComments(@PathVariable("pid") Integer id) {
		List<Map<String, String>> commentList = commentService.findByPid(id);
		return Msg.success().add("comments", commentList);
	}

	/**
	 * 删除评论信息
	 * 
	 * @param ids
	 * @return
	 */
	@PostMapping("/deleteComments")
	public Msg deleteComments(int[] ids) {
		boolean bool = commentService.batchDelete(ids);
		return Msg.judge(bool);
	}

	/**
	 * 新增评论信息
	 * 
	 * @param comments
	 * @return
	 */
	@PostMapping("/saveComments")
	public Msg saveComments(Comments comments) {
		DefaultTransactionDefinition def = new DefaultTransactionDefinition();
		def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
		TransactionStatus status = txManager.getTransaction(def);
		try {
			Bookcase b = new Bookcase();
			b.setBPicId(comments.getCPid());
			b.setBUserId(comments.getCUserid());
			b.setBType("2");// 购买
			 List<Bookcase> findByParam = bookService.findByParam(b);
			if (findByParam == null ||findByParam.isEmpty()) {
				txManager.rollback(status);
				return Msg.fail("未购买改商品,无法评论");
			}
			List<Comments> c = commentService.findByUserIdAndPid(comments.getCUserid(), comments.getCPid());
			if (c != null && !c.isEmpty()) {
				txManager.rollback(status);
				return Msg.fail("评论过,不可评论");
			}

			boolean bool = commentService.insertComments(comments);
			if (!bool) {
				txManager.rollback(status);
				return Msg.fail("添加评论失败");
			}
			if (comments.getCLevel() != null) {
				bool = yhService.addExp(comments.getCUserid(), 2);
				if (!bool) {
					txManager.rollback(status);
					return Msg.fail("添加经验失败");
				}
			}
			bool = yhService.addExp(comments.getCUserid(), 5);
			if (!bool) {
				txManager.rollback(status);
				return Msg.fail("添加经验失败");
			}
		} catch (Exception e) {
			txManager.rollback(status);
			e.printStackTrace();
			return Msg.fail(e.getMessage());
		}
		txManager.commit(status);
		return Msg.success();
	}

	@PostMapping("/updateComments")
	public Msg updateComments(Comments comments) {
		Comments c = commentService.findById(comments.getCId());
		if (c == null) {
			return Msg.fail();
		}
		Instant now = Instant.now();
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		LocalDateTime datetime = LocalDateTime.parse(c.getCTime(), formatter).plus(Duration.ofHours(24l));
		Instant parse = datetime.toInstant(ZoneOffset.ofHours(0));
		System.out.println(c.getCTime());
		System.out.println(parse.toString());
		if (now.isAfter(parse)) {
			return Msg.fail("超时,无法更改");
		}
		return Msg.success();

	}

}
